package com.fanqie.springboot2.mongo.service;

import com.fanqie.springboot2.mongo.entity.ExceptionLog;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author php
 * @date 2019/4/10
 */
@Service
public class ExceptionLogService {
    private final MongoTemplate mongoTemplate;

    @Autowired
    public ExceptionLogService(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    /**
     * 使用insert添加数据
     *
     * @param exceptionLog 实体信息
     * @return 保存后的信息
     */
    public ExceptionLog add(ExceptionLog exceptionLog) {
        return this.mongoTemplate.insert(exceptionLog);
    }

    /**
     * 插入集合
     *
     * @param list List<ExceptionLog>
     * @return List<ExceptionLog>
     */
    public List<ExceptionLog> addAll(List<ExceptionLog> list) {
        return (List<ExceptionLog>) this.mongoTemplate.insertAll(list);
    }

    /**
     * 根据主键进行删除
     *
     * @param openId 主键
     * @return 删除的数量
     */
    public long delete(String openId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(openId));
        DeleteResult remove = this.mongoTemplate.remove(query, ExceptionLog.class);
        return remove.getDeletedCount();
    }

    /**
     * 根据主键，修改实体
     *
     * @param exceptionLog 需要修改的信息
     */
    public void findAndModify(ExceptionLog exceptionLog) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(exceptionLog.getObjectId()));
        Update update = new Update();
        update.set("status", exceptionLog.getStatus());
        update.set("body", exceptionLog.getBody());
        this.mongoTemplate.findAndModify(query, update, ExceptionLog.class);
    }

    /**
     * 使用update方式修改数据
     *
     * @param exceptionLog 需要修改的信息
     */
    public void update(ExceptionLog exceptionLog) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(exceptionLog.getObjectId()));
        Update update = new Update();
        update.set("status", exceptionLog.getStatus());
        update.set("body", exceptionLog.getBody());
        this.mongoTemplate.updateFirst(query, update, ExceptionLog.class);
    }

    /**
     * 查询当前集合中所有的文档
     *
     * @return list
     */
    public List<ExceptionLog> findAll() {
        return this.mongoTemplate.findAll(ExceptionLog.class);
    }

    /**
     * 根据status升序获取List
     *
     * @return list
     */
    public List<ExceptionLog> find() {
        Query query = new Query();
        query.with(Sort.by(Sort.Order.asc("status")));
        return this.mongoTemplate.find(query, ExceptionLog.class);
    }

    /**
     * 查询状态小于404 的一个文档
     *
     * @return ExceptionLog
     */
    public ExceptionLog findOne() {
        Query query = new Query();
        query.addCriteria(Criteria.where("status").lt(404));
        return this.mongoTemplate.findOne(query, ExceptionLog.class);
    }
}
